package com.qf.forumproject.blog.controller;

import cn.hutool.core.date.DateUtil;
import com.qf.forumproject.entity.QfUser;
import com.qf.forumproject.utils.Result;
import com.qf.forumproject.utils.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.qf.forumproject.entity.QfComment;
import com.qf.forumproject.blog.service.QfCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (QfComment)控制层
 *
 * @author protagonist
 * @since 2022-07-26 22:42:37
 */
@RestController
@Slf4j
@RequestMapping("/qfComment")
@Api(tags = "用户评论接口")
public class QfCommentController {
    /**
     * 服务对象
     */
    @Resource
    private QfCommentService qfCommentServiceImpl;

    /**
     * 查询评论总条数
     * @return
     */
    @GetMapping("/getCommentCount")
    @ApiOperation("查询评论总条数")
    public Result getCommentCount(){
        long count = qfCommentServiceImpl.count();
        if (count > 0) {
            return new Result<>(true,StatusCode.OK,"评论总记录数查询成功",count);
        }
        return new Result<>(false,StatusCode.ERROR,"评论总记录数查询失败");
    }

    /**
     * 添加文章评论
     * @param qfComment 文章id从前端传过来的
     * @return
     */
    @GetMapping("/commit")
    @ApiOperation("添加文章评论")
    public Result commitComment(QfComment qfComment) {

        Subject subject = SecurityUtils.getSubject();
        QfUser user = (QfUser) subject.getPrincipal();

        qfComment.setUid(user.getId());
        qfComment.setGmtCreate(DateUtil.date());

        boolean save = qfCommentServiceImpl.save(qfComment);

        if (save) {
            return new Result<>(true,StatusCode.OK,"评论添加成功",qfComment);
        }
        return new Result<>(false,StatusCode.ERROR,"评论添加失败");
    }
}
